/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.barrientosjunior.blackup.persistence;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Vector;

/**
 *
 * @author Junior
 */
public class PreferencesManager {

    /**
     * Classe de preferencias da aplicação
     */
    public static class AppPreferences implements Serializable {
        private Vector<br.com.barrientosjunior.blackup.model.File> files;

        public AppPreferences() {
            this.files = new Vector<br.com.barrientosjunior.blackup.model.File>();
        }

        public Vector<br.com.barrientosjunior.blackup.model.File> getFiles() {
            return files;
        }

        public void setFiles(Vector<br.com.barrientosjunior.blackup.model.File> files) {
            this.files = files;
        }

    }

    private static final File PREFERENCES_FILE = new File("./blackup.preferences");

    /**
     * Salva em arquivo as preferências do programa.
     * @param preferences
     * @throws Exception
     */
    public static void savePreferences(AppPreferences preferences) throws Exception{
        ObjectOutputStream out = null;
        try{
            out = new ObjectOutputStream(new FileOutputStream(PREFERENCES_FILE));
            out.writeObject(preferences);
        } catch (IOException e){
            throw new Exception("Erro na gravação do arquivo de preferências",e);
        } finally {
            try{
                out.close();
            } catch (Exception e){
                
            }
        }
    }

    /**
     * Carrega o arqivo de preferências.
     * @return objeto com as preferências.
     * @throws Exception
     */
    public static AppPreferences loadPreferences() throws Exception{
        AppPreferences preferences = new AppPreferences();
        ObjectInputStream in = null;
        try{
            in = new ObjectInputStream(new FileInputStream(PREFERENCES_FILE));
            preferences = (AppPreferences) in.readObject();
        } catch (FileNotFoundException e){
            // caso arquivo não exista cria arquivo vazio
            return new AppPreferences();
        } catch (IOException e){
            throw new Exception("Erro na leitura do arquivo de preferências",e);
        } finally{
            try{
                in.close();
            } catch (Exception e){

            }
        }
        return preferences;
    }
    
}
